# encoding: utf-8
import os
import datetime
import logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template

from model.music import MusicWork, MusicMovement


class WorkIndexHandler(webapp.RequestHandler):
    def get(self):
        works = MusicWork.all().fetch(50)
        template_values = {
            'works': works,
        }
        path = os.path.join(os.path.dirname(__file__),
                            'template/work_index.html')
        self.response.out.write(template.render(path, template_values))


class WorkImportHandler(webapp.RequestHandler):
    def get(self):
        # A form for user to input the musician information.
        path = os.path.join(os.path.dirname(__file__),
                            'template/musician_new.html')
        self.response.out.write(template.render(path, {}))

    def post(self):
        # Save the musician then redirect to the detail page.
        m = Musician(name=self.request.get("name"),
                     biography = self.request.get("biography"),
                     birthdate = datetime.date.today())
        m.put()

        self.redirect('/music')


application = webapp.WSGIApplication(
    [('/work', WorkIndexHandler),
     ('/work/import', WorkImportHandler),],
    debug = True)


def main():
    run_wsgi_app(application)


if __name__ == "__main__":
    main()
